package com.fsquare.shipping.dao;

import java.util.List;

import javax.persistence.TypedQuery;

import org.broadleafcommerce.core.order.dao.FulfillmentOptionDaoImpl;
import org.broadleafcommerce.core.order.domain.FulfillmentOption;
import org.broadleafcommerce.core.order.fulfillment.domain.BandedPriceFulfillmentOption;
import org.broadleafcommerce.core.order.fulfillment.domain.FulfillmentPriceBand;
import org.springframework.stereotype.Repository;

@Repository("extFulfillmentOptionDao")
public class ExtFulfillmentOptionDaoImpl extends FulfillmentOptionDaoImpl implements ExtFulfillmentOptionDao {

	@Override
	public void deleteFulfillmentOption(final Long fulfillmentOptionId) {
		FulfillmentOption fulfillmentOption = readFulfillmentOptionById(fulfillmentOptionId);
		if(fulfillmentOption != null)
			em.remove(fulfillmentOption);
	}

	@Override
	public void deleteFulfillmentPriceBand(Long id) {
		FulfillmentPriceBand fulfillmentPriceBand = readFulfillmentPriceBandById(id);
		if(fulfillmentPriceBand != null)
			em.remove(fulfillmentPriceBand);
		
	}
	
	@Override
	public FulfillmentPriceBand save(FulfillmentPriceBand fulfillmentPriceBand) {
		return em.merge(fulfillmentPriceBand);
	}
	
	@Override
	public FulfillmentPriceBand readFulfillmentPriceBandById(Long id) {
		return em.find(FulfillmentPriceBand.class, id);
	}

	@Override
	public List<FulfillmentPriceBand> readFulfillmentOptionPricebandsByFulfillmentOption(Long fulfillmentOptionId) {
      TypedQuery<FulfillmentPriceBand> query = em.createNamedQuery("READ_FULFILLMENT_PRICEBAND_BY_FULFILLMENT_OPTION", FulfillmentPriceBand.class);
      query.setParameter("fulfillmentId", fulfillmentOptionId);
      return query.getResultList();
		
	}
	@Override
	public FulfillmentPriceBand createFulfillmentPriceBand() {
        return (FulfillmentPriceBand) entityConfiguration.createEntityInstance(FulfillmentPriceBand.class.getName());
    }
	
	@Override
	public FulfillmentOption createFulfillmentOption() {
        return (FulfillmentOption) entityConfiguration.createEntityInstance(FulfillmentOption.class.getName());
    }
	
	@Override
	public BandedPriceFulfillmentOption createBandedPriceFulfillmentOption() {
        return (BandedPriceFulfillmentOption) entityConfiguration.createEntityInstance(BandedPriceFulfillmentOption.class.getName());
    }

}
